





//
//  ZRMediaSubViewController.m
//  ZQLearn
//
//  Created by admin on 2019/4/19.
//  Copyright © 2019 admin. All rights reserved.
#import "ZInternationalViewController.h"
#import "InternationalCell.h"
#import "ZRMediaSubViewController.h"
#import "NewsCell.h"
#import "NewsModel.h"
#import "VideoCell.h"
#import "NNewsDetailViewController.h"
#import "ZVideoViewController.h"
#import "ZNewsDetailViewController.h"
@interface ZRMediaSubViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSInteger currentIndex;

///刷新和加载更多时候用
@property (nonatomic,assign)NSInteger currentPage;

///当前标题对应的id，用来请求列表数据的
@property (nonatomic,copy)NSString *currentIndexId;

@end

@implementation ZRMediaSubViewController


#define vCellIdentifier @"video"
#define internationalCellIdentifier @"interna"
#define cellIdentifier @"news"

-(UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 1.5, ScreenWidth , ScreenHeight - kTabHeight-ScreenHeightFlex-52) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //        _tableView.rowHeight = 95;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NewsCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        [_tableView registerNib:[UINib nibWithNibName:@"VideoCell" bundle:nil] forCellReuseIdentifier:vCellIdentifier];
        [_tableView registerNib:[UINib nibWithNibName:@"InternationalCell" bundle:nil] forCellReuseIdentifier:internationalCellIdentifier];
        WeakStament(ws);
        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
            [ws refreshData];
        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    //    self.view.backgroundColor = [UIColor brownColor];
    self.view.backgroundColor = kBackgroundColor;
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    self.currentIndex = 1;
    self.currentPage = 1;
    //    self.currentIndexId = 1;
    // Do any additional setup after loading the view.
}

#pragma mark - delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.currentIndex==1) {
        return 180.f;
    }else if (self.currentIndex==2){
        return 135.f;
    }else{
        return 95.f;
    }
    return 0.f;
}
#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.currentIndex>0) {
        
        return self.dataArray.count;
        
    }else{
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsCell *cell;
    VideoCell *videoCell;
    InternationalCell *internaCell;
    ////索引为0时候 是一个单独的控制器，所以这里的cell都是从索引1开始的，也就是从第二个开始的
    if (self.currentIndex==1) {
        //国际新闻
        internaCell = [tableView dequeueReusableCellWithIdentifier:internationalCellIdentifier forIndexPath:indexPath];
        if (self.dataArray.count>indexPath.row) {
            NewsModel *model = self.dataArray[indexPath.row];
            
            [internaCell setCellWithModel:model];
        }
        return internaCell;
        
    }else if(self.currentIndex==2){
        //视频
        videoCell = [tableView dequeueReusableCellWithIdentifier:vCellIdentifier forIndexPath:indexPath];
        
        if (self.dataArray.count>indexPath.row) {
            NewsModel *model = self.dataArray[indexPath.row];
            [videoCell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo]] placeholderImage:nil];
            videoCell.titleLab.text = model.title;
        }
        return videoCell;
    }else{
        //其他都是正常的
        NewsModel *model = self.dataArray[indexPath.row];
        cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
        [cell configCellWithData:model];
        return cell;
    }
    
    return nil;
    
}

#pragma delegate
#pragma mark -json串转换成数组
- (id)arrayWithJsonString:(NSString *)jsonString{
    
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData
                                                   options:NSJSONReadingMutableContainers
                                                     error:&err];
    
    if(err) {
        YYLog(@"json解析失败：%@",err);
        return nil;
    }
    return arr;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.currentIndex==1) {
        ///国际新闻
        NewsModel *model = self.dataArray[indexPath.row];
        YYLog(@"%@",model.atlasContent);
        ZInternationalViewController *interna = [ZInternationalViewController new];
        ///json 串 转 对象实体
       id obj =  [self arrayWithJsonString:model.atlasContent];
        YYLog(@"%@",obj);
        
        
        interna.iconArray = (NSArray *)obj;
        
        [self.navigationController pushViewController:interna animated:YES];
        
    }else if (self.currentIndex==2){
        
        NewsModel *model = self.dataArray[indexPath.row];
        MidModel *mModel = [MidModel new];
        mModel.name = model.title;
        mModel.ID = model.ID;
        //        mModel.price = model.price;
        mModel.picUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo];
        mModel.videoUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,model.videoUrl];
        YYLog(@"%@%@",mModel.picUrl,mModel.videoUrl);
        ZVideoViewController *video = [ZVideoViewController new];
        video.MModel = mModel;
        [self.navigationController pushViewController:video animated:YES];
        
    }else{
        //正常的新闻详情
        NewsModel *model = self.dataArray[indexPath.row];
        ZNewsDetailViewController *news = [ZNewsDetailViewController new];
        //        news.htmlString = model.content;
        news.model = model;
        [self.navigationController pushViewController:news animated:YES];
    }
}


#pragma mark ZJScrollPageViewChildVcDelegate
- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    
    YYLog(@"%ld",index);
    [UserModel shareInstance].tabIndex = index;
    self.currentIndex = index;
    
}
- (void)zj_viewDidAppearForIndex:(NSInteger)index{
    
}
- (void)zj_viewWillDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index{
    //这里具体处理请求逻辑不同的页面根据索引进行请求
    YYLog(@"%ld",index);
    self.currentIndex = index;
    //        [self loadDataWithIndex:index];
    if (self.datas.count>0) {
        
        NewsModel *model = self.datas[index];
        self.currentIndexId = model.ID;
        [self loadDataWithId:model.ID];
        YYLog(@"%@",model.ID);
        
    }
    
}
- (void)loadDataWithId:(NSString *)Id{
    
    if (self.currentPage==1) {
        [self.dataArray removeAllObjects];
    }
    NSDictionary *dic = @{@"columnInfo.id":Id,@"pageSize":@(20),@"pageIndex":@(1)};
    WeakStament(ws);
    [ZNetworkService postRequestWithBaseUrl:kNewsListUrl setParameters:dic success:^(id result) {
        
        YYLog(@"%@",result);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
            for (NSDictionary *dic in arr) {
                NewsModel *model = [NewsModel mj_objectWithKeyValues:dic];
                [ws.dataArray addObject:model];
            }
            [ws.tableView reloadData];
            [ws endRefresh];
        }else{
            [AFToast showText:result[@"errorMsg"]];
            [ws endRefresh];
        }
        
    } fail:^(NSError *error) {
        
        YYLog(@"%@",error);
        [self endRefresh];
        
    } delegater:nil];
}

- (void)refreshData{
    //    self.currentPage=1;
    //    self.currentIndexId = [UserModel shareInstance].Id;
    self.currentPage = 1;
    [self loadDataWithId:self.currentIndexId];
}

- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)loadMoreData{
    
}

@end
